package com.example.gestureactivity2;

import com.example.gestureactivity2.utils.Logger;
import com.example.gestureactivity2.view.GestureView;
import com.example.gestureactivity2.view.PageLoader;
import com.example.gestureactivity2.view.PageLoader.OnFooterRefreshListener;
import com.example.gestureactivity2.view.PageLoader.OnHeaderRefreshListener;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends BaseActivity implements
		OnHeaderRefreshListener, OnFooterRefreshListener {

	private static final String	TAG	= "WebViewActivity";
	private WebView				webView;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_webview);

		initView();
	}

	private void initView() {
		GestureView mLayout = (GestureView) findViewById(R.id.layout_web);
		mLayout.setOnFlingListener(this);
		webView = (WebView) findViewById(R.id.wv_activity_webview);
		webView.setWebViewClient(new MyWebViewClient());
		webView.loadUrl(buildUrl(mUrl));

		mPageLoader = (PageLoader) findViewById(R.id.pageloader_activity_web);
		mPageLoader.setOnHeaderRefreshListener(this);
		mPageLoader.setOnFooterRefreshListener(this);
	}

	@Override
	public void onFooterRefresh(PageLoader view) {
		webView.loadUrl(buildUrl(mUrl));
	}

	@Override
	public void onHeaderRefresh(PageLoader view) {
		webView.loadUrl(buildUrl(mUrl));
	}

	class MyWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return super.shouldOverrideUrlLoading(view, url);
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			mPageLoader.onHeaderRefreshComplete();
			mPageLoader.onFooterRefreshComplete();
		}
	}

	private static String		mUrl;
	private static final String	url1	= "http://www.baidu.com";
	private static final String	url2	= "http://www.eoeandroid.com/thread-252572-1-1.html";
	private PageLoader			mPageLoader;

	private String buildUrl(String url) {
		if (null == url || "".equalsIgnoreCase(url)) {
			mUrl = url1;
		} else if (null != url && url1.equalsIgnoreCase(url)) {
			mUrl = url2;
		} else if (null != url && url2.equalsIgnoreCase(url)) {
			mUrl = url1;
		}
		Logger.e(TAG, mUrl);
		return mUrl;
	}
}
